package com.zyk.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangsan
 * @date 2021/4/7 15:43
 */
public class M0108 {

    // https://leetcode-cn.com/problems/zero-matrix-lcci/
    // 暴力解, 遍历一遍统计 出现0的 所在行与列
    public static void setZeroes(int[][] matrix) {
        if (matrix.length == 0 || matrix[0].length == 0) return;
        List<int[]> zeroLocation = new ArrayList<>();
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 0; c < matrix[0].length; c++) {
                if (matrix[r][c] == 0) {
                    zeroLocation.add(new int[]{r, c});
                }
            }
        }
        for (int[] location : zeroLocation) {
            int R = location[0];
            int C = location[1];
            for (int c = 0; c < matrix[0].length; c++) {
                matrix[R][c] = 0;
            }
            for (int r = 0; r < matrix.length; r++) {
                matrix[r][C] = 0;
            }
        }
    }


    // for test
    public static void main(String[] args) {
        int[][] matrix = {
                {0,1,2,0},
                {3,4,5,2},
                {1,3,1,5}
        };
        setZeroes(matrix);

        for (int[] arr : matrix) {
            System.out.println(Arrays.toString(arr));
        }
    }


}
